package KNNCalculate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

/**
 * Created by Administrator on 2016/12/3 0003.
 * 此算法用于获得文字出现的频率并将其存储到矩阵中（HashMap<String,ArrayList<Short>> wordfreq）
 */
public class AnswerList {
    public static final int classNumber=2;//答案分为2类
    public static final int ListNumber=3;//每类有3个例子
    public static final int num=6;
    public static int row;
    public HashMap<String,ArrayList<Short>> wordfreq=new HashMap<String,ArrayList<Short>>();
    public static final String[] AnswerListNo={
            "眼睛明亮健康身体发达",
            "高大身材胳膊勇猛四肢",
            "胳膊健康身体高大健康"
    };
    public static final String[] AnswerListYes={
            "美国很发达",
            "美国经济富强",
            "日本富强科技发达"
    };
    public void freqOfList(String [] List,int category){
        for(int n=0;n<List.length;n++){
            String[] words=List[n].split("");
            int i=n+category*3;
            for(int j=0;j<words.length;j++){
                if(!wordfreq.containsKey(words[j])){
                    ArrayList<Short> al=new ArrayList<Short>();
                    for(int x=0;x<row;x++) al.add((short)0);
                    al.set(i,(short)1);
                    wordfreq.put(words[j], al);
                }else{
                    ArrayList<Short> al=wordfreq.get(words[j]);
                    short freq=al.get(i);
                    freq++;
                    al.set(i,(short)(freq));
                }
            }
        }
    }
    public HashMap<String,ArrayList<Short>> extractMatrix(String [] first,String []second){
        this.row=first.length+second.length;
        this.freqOfList(first,0);
        this.freqOfList(second,1);
        return this.wordfreq;
    }
    public HashMap<String,ArrayList<Short>> extractMatrix(String first){
        this.row =2;//row为1时，提取的向量为0，所以不能为1
        String[]res=new String[1];
        res[0]=first;
        this.freqOfList(res,0);
        return this.wordfreq;
    }
}

